<?php
/**
 * Created by JetBrains PhpStorm.
 * User: porterzhang
 * Date: 13-3-5 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() { }

    function output() {
        $variable = array();
        //判断是否缺少参数
        if (empty($_GET['name']) || empty($_GET['enname']) || empty($_GET['version']) || empty($_GET['integraltype'])) {
            $variable = array(
                'code' => 1001,
                'messagestr' => lang('plugin/cloud', '1001'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        //判断是否已经存在相同名称的应用
        $appList = C::t('#cloud#cloud_app')->fetch_first_by_name($_GET['name']);
        if (!empty($appList)) {
            $variable = array(
                'code' => 2000,
                'messagestr' => lang('plugin/cloud', '2000'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }
        $app = array(
            'name' => $_GET['name'],
            'enname' => $_GET['enname'],
            'discription' => empty($_GET['discription']) ? '' : $_GET['discription'],
            'disabled' => 1,
            'version' => $_GET['version'],
            'lasttime' => TIMESTAMP,
            'integraltype' => 2,
            'appStoreId' => $_GET['appStoreId'],
            'jpushkey' => $_GET['jpushkey'],
            'jpushSecret' => $_GET['jpushSecret'],
        );
        $caid = C::t('#cloud#cloud_app')->insert($app);
        //如果插入失败，这返回错误信息
        if (empty($caid)) {
            $variable = array(
                'code' => 2001,
                'messagestr' => lang('plugin/cloud', '2001'),
            );
            cloud_core::result(cloud_core::variable($variable));
        }

        //设置应用的功能
        //cfids
       // $fid_array = explode(",", $_GET['cfids']);
        $fid_array = C::t('#cloud#cloud_function')->function_status_all_and_ftype_find();
        foreach($fid_array as $key=>$val){
            $data= array(
                'fid'=>$val['id'],
                'caid'=>$caid,
                'createtime'=>TIMESTAMP,
            );
            C::t('#cloud#cloud_app_function')->insert($data);
        }
        //创建应用角色
        $data_role=array(
            'name'=>'应用管理员组',
            'status' =>1,
            'createtime' =>TIMESTAMP,
            'createtype'=>0,
            'caid' =>$caid,
        );
        $rold_id=C::t('#cloud#cloud_app_role')->insert($data_role);

        //给应用管理员组，付公共的权限
        foreach ($fid_array as $key => $value) {
            $app_function=C::t('#cloud#cloud_function')->fetch_function_info($value['id']);
            if($app_function['ftype']==1){
                $data=array(
                    'rid'=>$rold_id,
                    'fid'=>$app_function['id'],
                    'fpid'=>$app_function['pid'],
                    'name'=>$app_function['name'],
                    'linkurl'=>$app_function['linkurl'],
                    'icon'=>$app_function['icon'],
                    'sort'=>$app_function['sort'],
                );
                C::t('#cloud#cloud_role_purview')->insert($data);
            }
        }


        $salt = substr(uniqid(rand()), -6);
        //同时新增一个app管理员
        $app_manage = array(
            'name' => 'admin',
            'password' => md5(md5('123456') . $salt),
            'lasttime' => TIMESTAMP,
            'salt' => $salt,
            'lastip' => '',
            'caid' => $caid,
            'status' => 1,
            'carid'=>$rold_id,
        );
        C::t('#cloud#cloud_app_manage')->insert($app_manage);
        //正常返回
        $variable = array(
            'messagestr' => lang('plugin/cloud', '2002'),
        );
        cloud_core::result(cloud_core::variable($variable));
    }
}

?>

